package com.xing.binbackend.pattern.proxy21.beauty.staticp;

import com.xing.binbackend.pattern.proxy21.beauty.model.Request;
import com.xing.binbackend.pattern.proxy21.beauty.model.UserVo;

/**
 * 如果类没有借口，那就直接用继承的方式
 */
public class UserServiceProxy2 extends UserService {
    private final MetricsCollector metricsCollector;

    public UserServiceProxy2(MetricsCollector metricsCollector) {
        this.metricsCollector = metricsCollector;
    }

    public UserVo login(String telephone, String password) {
        // 代理
        UserVo userVo = super.login(telephone, password);
        long startTimestamp = System.currentTimeMillis();
        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;
        Request request = new Request("login", responseTime, startTimestamp);
        metricsCollector.recordRequest(request);
        return userVo;
    }

    public UserVo register(String telephone, String password) {
        // 代理
        UserVo userVo = super.register(telephone, password);
        long startTimestamp = System.currentTimeMillis();
        long endTimeStamp = System.currentTimeMillis();
        long responseTime = endTimeStamp - startTimestamp;
        Request request = new Request("register", responseTime, startTimestamp);
        metricsCollector.recordRequest(request);
        return userVo;
    }
}
